#!/usr/bin/python
# -*- coding: utf-8 -*-

from grid import Grid, RealSettings, RealKoeffs
from lines import Lines, Curve
from drawer import Drawer


class DynPlot:
    def __init__(self, realSettings, realKoeffs, curves):
        self.grid = Grid(realSettings, realKoeffs)
        self.lines = Lines()
        self.lines.set_sample_width(self.grid.get_x_sample_width())
        self.set_curves(curves)
        self.drawer = Drawer(self.grid.get_sample_settings())
        self.index = -1

    def set_index(self, index):
        self.index = index

    def draw(self, dc, indexTime):
        self.drawer.update_dc(dc)
        shift = (len(self.get_curves()[0].mass) - self.index)*2
        self.grid.draw(self.drawer, indexTime - shift)
        self.lines.draw(self.drawer, self.index)

    def set_curves(self, curves):
        self.lines.set_curves(curves)

    def get_curves(self):
        return self.lines.get_curves()

    def legends_visible(self, flag):
        self.lines.legends_visible(flag)
        self.drawer.additionOffsets = self.lines.get_legends_offset()

    def set_koeffs(self, realKoeffs):
        self.grid.set_koeffs(realKoeffs)
        self.drawer.update_settings(self.grid.get_sample_settings())
        self.lines.set_sample_width(self.grid.get_x_sample_width())

    def get_koeffs(self):
        return self.grid.get_koeffs()

    def set_settings(self, realSettings):
        self.grid.set_real_settings(realSettings)
        self.drawer.update_settings(self.grid.get_sample_settings())
        self.lines.set_sample_width(self.grid.get_x_sample_width())

    def get_settings(self):
        return self.grid.get_real_settings()

    def scale(self, koeff, mousePos):
        settings = self.get_settings()
        settings.yRealMax *= koeff
        settings.yRealMin *= koeff
        settings.xRealWidth *= koeff
        settings.xRealStep *= koeff
        settings.yRealStep *= koeff
        self.set_settings(settings)


if __name__ == "__main__":
    execfile("test_dyn_plot.py")

